package structures.hashtab;

import java.util.Scanner;

public class HashTabDemo {
    private static final int SIZE = 8;
    public static void main(String[] args) {
        // 创建队列
        HashTab hashTab = new HashTab(SIZE);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println();
            System.out.println("s(show) 显示队列全部数据");
            System.out.println("e(exit) 退出程序");
            System.out.println("a(add) 添加数据");
            System.out.println("d(delete) 删除数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    hashTab.list();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    int name = scanner.nextInt();
                    hashTab.add(new Employ(name, "name" + name));
                    break;
                case 'd':
                    int id = scanner.nextInt();
                    boolean deleteFlag = hashTab.delete(id);
                    if (deleteFlag) {
                        System.out.println("--- 删除成功 ---");
                    } else {
                        System.out.println("--- 删除失败 ---");
                    }
                    break;
            }
            System.out.println("------------------------");
        }
    }
}

class HashTab {
    private final EmployLinkList[] employLinkLists;
    private final int size;

    public HashTab(int size) {
        this.size = size;
        if (size <= 0) {
            throw new IllegalArgumentException("请输入size > 0 的值");
        }
        employLinkLists = new EmployLinkList[size];
        for (int index = 0; index < employLinkLists.length; index++) {
            employLinkLists[index] = new EmployLinkList();
        }
    }

    /**
     * 添加
     * @param employ
     */
    public void add(Employ employ) {
        employLinkLists[hashFun(employ.getId())].add(employ);
    }

    /**
     * 遍历
     */
    public void list() {
        for (EmployLinkList employLinkList : employLinkLists) {
            employLinkList.list();
            System.out.println("-------------------------");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean delete(int id) {
        return employLinkLists[hashFun(id)].delete(id);
    }

    /**
     * 散列最简单的实现方式  取模...
     * @param id
     * @return
     */
    public int hashFun(int id) {
        return id % size;
    }
}

class EmployLinkList {
    private Employ head;

    /**
     * 增(默认尾部添加数据)
     * @param employ
     */
    public void add(Employ employ) {
        if (head == null) {
            head = employ;
            return;
        }

        Employ node = head;
        while (node.getNext() != null) {
            node = node.getNext();
        }

        node.setNext(employ);
    }

    /**
     * 删
     * @param id
     * @return
     */
    public boolean delete(int id) {
        if (head == null) {
            return false;
        }

        if (head.getId() == id) {
            head = head.getNext();
            return true;
        }

        Employ node = head;
        while (node.getNext() != null) {
            if (node.getNext().getId() == id) {
                node.setNext(node.getNext().getNext());
                return true;
            }
            node = node.getNext();

        }
        return false;
    }

    /**
     * 遍历
     */
    public void list() {
        Employ node = head;

        while (node != null) {
            System.out.println(node);
            node = node.getNext();
        }
    }
}

class Employ {
    private int id;
    private String name;
    private Employ next;

    public Employ(int id, String name) {
        if (id < 0) {
            throw new IllegalArgumentException("请输入id >= 0 的雇员信息");
        }
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employ getNext() {
        return next;
    }

    public void setNext(Employ next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Employ{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
